package com.woniuxy.house.mapper;

import com.woniuxy.house.entity.Source;
import com.woniuxy.house.entity.UserAndSource;
import com.woniuxy.house.provider.SourceSqlProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 *
 * 操作房源数据的接口
 * @author baobo
 * @date 2022-08-27-18:29
 */
public interface SourceMapper {
	/**
	 * 新增房源
	 * @param source
	 */
	@Insert("insert into house_source" +
			"(title,province,city,area,street,price," +
			"towards,size,room_style,publish_time,rental_house_way," +
			"house_style,rental_time_way,tag,longitude,latitude," +
			"isrental,iscross,user_id,source_detail_id,ju_shi)" +
			"values(#{title},#{province},#{city},#{area},#{street},#{price}," +
			"#{towards},#{size},#{roomStyle},#{publishTime},#{rentalHouseWay}," +
			"#{houseStyle},#{rentalTimeWay},#{tag},#{longitude},#{latitude}," +
			"#{isrental},#{iscross},#{user.id},#{houseDetail.id},#{juShi})")
	@Options(useGeneratedKeys=true ,keyColumn="id" ,keyProperty="id")
	void add(Source source);

	/**
	 * 通过改变房源是否已出租的状态来删除房源
	 * @param id
	 */
	@Update("update  house_source set isrental='y' where id=#{id}")
	void delete(Integer id);

	/**
	 * 根据字段是否为空更新房源信息
	 * @param source
	 */
	@UpdateProvider(value = SourceSqlProvider.class,method = "update")
	void update(Source source);

	/**
	 * 根据房源id查询房源
	 * @param id
	 * @return
	 */
	@Select("select * from house_source where id=#{id}")
	@Results(id="SourceResultMapping",value = {
			@Result(column = "id",property = "id",id = true),
			@Result(column = "title",property = "title"),
			@Result(column = "province",property = "province"),
			@Result(column = "city",property = "city"),
			@Result(column = "area",property = "area"),
			@Result(column = "street",property = "street"),
			@Result(column = "price",property = "price"),
			@Result(column = "towards",property = "towards"),
			@Result(column = "size",property = "size"),
			@Result(column = "room_style",property = "roomStyle"),
			@Result(column = "ju_shi",property = "juShi"),
			@Result(column = "publish_time",property = "publishTime"),
			@Result(column = "rental_house_way",property = "rentalHouseWay"),
			@Result(column = "house_style",property = "houseStyle"),
			@Result(column = "rental_time_way",property = "rentalTimeWay"),
			@Result(column = "tag",property = "tag"),
			@Result(column = "longitude",property = "longitude"),
			@Result(column = "latitude",property = "latitude"),
			@Result(column = "isrental",property = "isrental"),
			@Result(column = "iscross",property = "iscross"),
			@Result(column = "user_id",property = "user",one = @One(select = "com.woniuxy.house.mapper.UserMapper.getById",fetchType = FetchType.LAZY)),
			@Result(column = "source_detail_id",property = "houseDetail",one = @One(select = "com.woniuxy.house.mapper.HouseDetailMapper.selectById",fetchType = FetchType.LAZY)),
			@Result(column = "id",property = "housePictures",many = @Many(select = "com.woniuxy.house.mapper.HousePicturesMapper.selectBySourceId",fetchType = FetchType.LAZY)),
			@Result(column = "id",property = "houseConfig",many = @Many(select = "com.woniuxy.house.mapper.ConfigMapper.getBySid",fetchType = FetchType.LAZY))

	})
	Source getById(Integer id);

	/**
	 * 多条件查询房源
	 * @param source
	 * @return
	 */
	@SelectProvider(value = SourceSqlProvider.class,method = "getSourceListByCondition")
	@ResultMap("SourceResultMapping")
	List<Source> getSourceListByCondition(Source source);/**
	 * 通过用户id查房源
	 * @param userId
	 * @return
	 */
	@Select("select * from house_source where user_id=#{userId} and isrental='n'")
	@ResultMap("SourceResultMapping")
	List<Source> getByUserId(Integer userId);

	/**
	 * 查出所有房源
	 */
	@Select("select* from house_source" )
	@ResultMap("SourceResultMapping")
	List<Source> getAll();
	/**@魏锦鹏
	 * 增加房源关注
	 */


	@Update("update house_source set isrental='y' where id=#{sid} and user_id=#{uid}")
	void deleteByUserIdAndSourceId(@Param("uid")Integer uid ,@Param("sid") Integer sid);
	@Insert("insert into house_user_source (user_id,source_id) values(#{userId},#{SourceId})")
	void addUserAndSource(@Param("userId") Integer userId,@Param("SourceId") Integer SourceId);
	/**@魏锦鹏
	 * 查房源关注
	 */
	@Select("select*from house_user_source where user_id=#{userId} and source_id =#{SourceId}")
	UserAndSource getUserAndSource(@Param("userId") Integer userId, @Param("SourceId") Integer SourceId);
	/**@魏锦鹏
	 * 取消房源关注
	 */
	@Delete("delete from house_user_source where user_id=#{userId} and source_id =#{SourceId}")
	void deleteByUidAndSid(@Param("userId") Integer userId, @Param("SourceId") Integer SourceId);
	/**@魏锦鹏
	 * 根据用户id得到所有关注的房源
	 * */
	@Select("select*from house_user_source u , house_source s  where u.user_id=#{userId} and u.source_id=s.id")
	@ResultMap("SourceResultMapping")
	List<Source> getMyConcern(Integer userId);
}
